package com.spirng.leetcode.day07;

public class Test98 {
    public static void main(String[] args) {
        Test98 test98 = new Test98();
        TreeNode treeNode = new TreeNode(5,new TreeNode(1),new TreeNode(4,new TreeNode(3),new TreeNode(6)));
        test98.isValidBST(treeNode);
    }
    TreeNode max;
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        // 左
        boolean left = isValidBST(root.left);
        if (!left) {
            return false;
        }
        // 中
        if (max != null && root.val <= max.val) {
            return false;
        }
        max = root;
        // 右
        boolean right = isValidBST(root.right);
        return right;
    }
    public boolean isValidBST1(TreeNode root) {
        if(root==null){
            return true;
        }
        boolean left=isValidBST1(root.left);
        if(!left){
            return false;
        }
        if(max!=null&&root.val>=max.val){
            return true;
        }
        max=root;
        boolean right=isValidBST1(root.right);
        return  right;
    }
}
